<?php

/**
 * 订单打印
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */


//查询订单主表类
quanli_require('module/base/bill/billSearch.class.php');

class ajax_orderPrint {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //列表
                    $this->orderPrintList();
                    break;

                case 2: //打印订单后 改变主表bill_master的打印次数
                    $this->changePrintNum();
                    break;

                case 3: //根据查询条件打印订单
                    $this->orderPrintAll();
                    break;
                case 4: //打印配送任务清单后 改变主表bill_master的打印次数
                    $this->changeTaskPrintNum();
                    break;
                case 5: //判断订单是否是澳粒订单
                    $this->check_AL();
                    break;
                case 6: //澳粒订单打印
                    $this->print_AL();
                    break;
                case 7: //判断订单是否是自营订单
                    $this->checkSelforder();
                    break;
                case 8://判断订单是否下午茶订单
                    $this->check_dunch();
                    break;
                case 9://下午茶订单打印
                    $this->print_dunch();
                    break;
            }
        }
    }

    /*
     * 列表
     *
     * */
    public function orderPrintList() {

        global $G_PGLOB;

        //查询条件选择了城市、配送站 则根据选择的城市、配送站查询
        if( isset($_REQUEST['deliver_cityid']) && is_numeric($_REQUEST['deliver_cityid']) && isset( $_REQUEST['distribution_id']) && is_numeric($_REQUEST['distribution_id'])){
            $_REQUEST['deliver_cityid'] = $_REQUEST['deliver_cityid'];
            $_REQUEST['distribution_id'] = $_REQUEST['distribution_id'];

            //查询条件选择了城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的配送站查询
        }else if(isset($_REQUEST['deliver_cityid']) && is_numeric($_REQUEST['deliver_cityid']) && !isset( $_REQUEST['distribution_id']) || !is_numeric($_REQUEST['distribution_id']) ){
            $_REQUEST['deliver_cityid'] = $_REQUEST['deliver_cityid'];
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有选择城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的城市、配送站查询
        }else{
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }
        }

        //实例用户类
        $billSearch = new billSearch();

        //查询订单信息
        $bill_array = $billSearch->searchQueryBill( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'] ,
            'rows' => array()
        );

        if ($bill_array) {

            foreach ($bill_array as &$bill) {

                //过滤顺丰马卡龙订单 Add By allen.qiang 2015-05-07
                if(date('Y-m-d') > '2015-05-06' && $bill['billPromotion']=='495' )
                    continue;

                //订单标识
                if($bill['is_urgent'] == 1){
                    $isUrgent = "加急";
                }else if($bill['is_urgent'] == 2){
                    $isUrgent = "重要";
                }else{
                    $isUrgent = "普通";
                }

                if($bill['billType'] == 1){
                    $billType = "正常订单";
                }else if($bill['billType'] == 2){
                    $billType = "生日会蛋糕";
                }else if($bill['billType'] == 3){
                    $billType = "市场推广";
                }else if($bill['billType'] == 4){
                    $billType = "其他订单";
                }else if($bill['billType'] == 5){
                    $billType = "补偿订单";
                }else if($bill['billType'] == 6){
                    $billType = "测试订单";
                }else if($bill['billType'] == 7){
                    $billType = "招待赠送";
                }else if($bill['billType'] == 8){
                    $billType = "大客户订单";
                }else{
                    $billType = "试吃订单";
                }
                //mod by yim.hu 20141121 FUN-BK-0035-订单状态优化 --start
//                 if($bill['bill_status'] == 0){
//                     $bill_status = "建立";
//                 }else if($bill['bill_status'] == 1){
//                     $bill_status = "确认";
//                 }else if($bill['bill_status'] == 2){
//                     $bill_status = "生产";
//                 }else if($bill['bill_status'] == 3){
//                     $bill_status = "入库";
//                 }else if($bill['bill_status'] == 4){
//                     $bill_status = "出库";
//                 }else if($bill['bill_status'] == 5){
//                     $bill_status = "配送站收货";
//                 }else if($bill['bill_status'] == 6){
//                     $bill_status = "配送";
//                 }else if($bill['bill_status'] == 7){
//                     $bill_status = "完成";
//                 }
                if($bill['bill_status'] == 0){
                	$bill_status = "未确认";
                }else if($bill['bill_status'] == 1){
                	$bill_status = "已确认";
                }else if($bill['bill_status'] == 2){
                	$bill_status = "已打印生产单";
                }else if($bill['bill_status'] == 3){
                	$bill_status = "已质检入库";
                }else if($bill['bill_status'] == 4){
                	$bill_status = "已上车";
                }else if($bill['bill_status'] == 5){
                	$bill_status = "已下车";
                }else if($bill['bill_status'] == 6){
                	$bill_status = "已出仓";//暂时没
                }else if($bill['bill_status'] == 7){
                	$bill_status = "已完成";
                }
                //mod by yim.hu 20141121 FUN-BK-0035-订单状态优化 --end
                if($bill['pay_type'] == 1){
                    $payTypeName = $bill['payTypeName'];
                }else{
                    $payTypeName = "";
                }

                //add by sunqiang 下午茶“我要请客订单状态判断” start

                $treat_bill_mark = "";

                if( $bill['billPlatform'] ==3 && $bill['bill_status']==1 )
                {

                    $treat_bill_info = new entity('dunch_treat');
                    $treat_bill_info->bill_no = $bill['bill_no'];
                    if($G_PGLOB->DBCA->fetch($treat_bill_info))
                    {
                        if($treat_bill_info->billMark != 1)
                        {
                            $treat_bill_mark = ' style="color:red;" ';
                        }

                    }
                }


                //add by sunqiang 下午茶“我要请客订单状态判断” end



                $amountReceivable = $bill['bill_totalPaid'] + $bill['bill_accessories_money'] + $bill['deliver_fee'] - $bill['bill_subtract_money'] - $bill['bill_coupon_money'] -$bill['bill_card_money'] -$bill['bill_points_money'];

                $result_array['rows'][] = array(
                    'id' => (string)$bill['bill_no'],
                    'cell' => array(
                        '<a '. $treat_bill_mark .' href="javascript: void(0);" onclick="orderPrint.billview(\''.$bill['bill_no'].'\');">'.$bill['bill_code'].'</a>',
                        $isUrgent,
                        $billType,
                        $bill_status,
                        $bill['print_num'],
                        $bill['print_time'],
                        $bill['printTaskList_num'],
                        $bill['send_date'],
                        $bill['send_start_time'],
                        $bill['send_end_time'],
                        $bill['distribution_name'],
                        $bill['distribution_technicia'],
                        $bill['deliver_city'],
                        $bill['deliver_county'],
                        $bill['deliver_address'],
                        $bill['deliver_name'],
                        $bill['deliver_mobile'],
                        $bill['deliver_phone'],
                        $bill['invoice'] == 1 ? '是' : '否',
                        $bill['bill_totalAmount'],
                        $payTypeName,
                        $bill['bill_receivable_money'],
                        $bill['remark']
                    )
                );

            }
        }

        echo json_encode( $result_array );

    }

    /**
     *  打印订单修改订单打印次数
     */
    public function changePrintNum(){

        global $G_PGLOB;

        //$_REQUEST['printBillNoArr']订单商品流水号数组
        if (!isset($_REQUEST['printBillNoArr'][0])) {
            returnJsonData( '参数错误',400);
        }

        $bill_array = $_REQUEST['printBillNoArr'];

        foreach($bill_array as &$bill_no) {

            $bill_obj = new entity('bill_master');
            $bill_obj ->bill_no = $bill_no;
            if(!$G_PGLOB->DBCA->fetch($bill_obj)){
                returnJsonData( '订单商品不存在' , 404 );
            }

            //避免异步协同操作更新订单状态 add by allen.qiang 2015-06-01 Start

            $print_operator = $G_PGLOB->currentUserID;
            $print_time = $G_PGLOB->sys_datetime;
            $print_num =  $bill_obj->print_num + 1;
            $bill_status = $bill_obj->bill_status;

            $update_sql = "UPDATE bill_master SET print_num = {$print_num} ,print_operator = '{$print_operator}', print_time = '$print_time' WHERE bill_no = $bill_no";

            $result = $G_PGLOB->DBCA->stmt_query($update_sql);

            if (!$result) {
                returnJsonData('订单打印失败', 404);
            }

            /*=======================================================
           * 写入日志 start
           * ======================================================
           */
            quanli_include('module/base/customer/logHandler.class.php');
            //实例日志类
            $logHandler = new logHandler( $G_PGLOB->DBCA );

            $log_array = array(
                'logCategoryID' => 1,
                'logItem' => $bill_no,
                'extend_1' => '订单打印',
                'extend_2' => '配送单已打印' . $print_num . '次',
                'extend_5' => $bill_status,
                //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --start
                //'operator' => $G_PGLOB->currentUserID
                'operator' => $print_operator
                //mod by allen.qiang 20141023 系统中操作信息中“操作人”切换为中文 --end

            );

             $logHandler->saveLogInfo( $log_array );
            /*=======================================================
             * 写入日志 end
             * ======================================================
             */

            //避免异步协同操作更新订单状态 add by allen.qiang 2015-06-01 End

        }

        returnJsonData( 'ok', 200 );

    }

    /*
     *
     * 根据查询条件打印配送任务清单
     */
    private function orderPrintAll(){

        //查询条件选择了城市、配送站 则根据选择的城市、配送站查询
        if( isset($_REQUEST['formValue']['deliver_cityid']) && is_numeric($_REQUEST['formValue']['deliver_cityid']) && isset( $_REQUEST['formValue']['distribution_id']) && is_numeric($_REQUEST['formValue']['distribution_id'])){
            $_REQUEST['formValue']['deliver_cityid'] = $_REQUEST['formValue']['deliver_cityid'];
            $_REQUEST['formValue']['distribution_id'] = $_REQUEST['formValue']['distribution_id'];

            //查询条件选择了城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的配送站查询
        }else if(isset($_REQUEST['formValue']['deliver_cityid']) && is_numeric($_REQUEST['formValue']['deliver_cityid']) && !isset( $_REQUEST['formValue']['distribution_id']) || !is_numeric($_REQUEST['formValue']['distribution_id']) ){
            $_REQUEST['formValue']['deliver_cityid'] = $_REQUEST['formValue']['deliver_cityid'];
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['formValue']['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['formValue']['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有选择城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的城市、配送站查询
        }else{
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['formValue']['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['formValue']['distribution_id'] = $distribution_id_array[0];

            }
        }


        global $G_PGLOB;
        $bill_no_array = array();//订单号数组
        $billSearch = new billSearch();
        //查询订单信息
        $temp_array  = $billSearch->searchQueryBill( $_REQUEST['formValue']  );

        if( empty($temp_array) ){
            returnJsonData( '暂无订单数据',400);
        }

        $print_array = array();//打印数组信息

        foreach( $temp_array as $key=>$val){

            $bill_no_array[] = $val['bill_no'];

            //订单查询类
            $bill_arraySearch = new billSearch();

            $arg_array = array(
                'bill_no'=>$val['bill_no']
            );
            //查询订单商品表 bill_goods
            $bill_goods_array = $bill_arraySearch->searchBillGoods( $arg_array );
//            echo '<pre>';
//            print_r( $bill_goods_array);exit;
            $birthday_card_array = array();
            $candle_amount_array = array();
            if( $bill_goods_array ){
                foreach( $bill_goods_array as $v){
                    if($v['birthday_card']){
                        $birthday_card_array[] = $v['birthday_card'];
                    }
                    $candle_amount_array[] = $v['candle_amount']>0?'是':'否';
                }
            }
            if( $birthday_card_array ){
                $birthday_card_str = implode(',',$birthday_card_array );
                $temp_array[$key]['birthday_card_str'] = $birthday_card_str;
            }

            if( $candle_amount_array ){
                $candle_amount_str = implode(',',$candle_amount_array);
                $temp_array[$key]['candle_amount_str'] =$candle_amount_str;
            }
        }


        $G_PGLOB->smarty->assign_by_ref('temp_array', $temp_array);
        $print_data = $G_PGLOB->smarty->fetch('send/orderTaskList.tpl');

        $return_array = array(
            'bill_no_array' => $bill_no_array,
            'print_data'=>$print_data
        );
        returnJsonData('ok',200,$return_array );
    }


    /**
     *  打印配送任务清单修改订单打印次数
     */
    public function changeTaskPrintNum(){

        global $G_PGLOB;

        if (!isset($_REQUEST['printBillNoArr'][0])) {
            returnJsonData( '参数错误',400);
        }

        $bill_array = $_REQUEST['printBillNoArr'];

        foreach($bill_array as &$bill_no) {

            $bill_obj = new entity('bill_master');
            $bill_obj ->bill_no = $bill_no;
            if(!$G_PGLOB->DBCA->fetch( $bill_obj )){
                returnJsonData( '订单商品不存在' , 404 );
            }
            $bill_obj->printTaskList_num = $bill_obj->printTaskList_num +1;//打印配送任务清单+1
            $bill_obj->printTaskList_operator = $G_PGLOB->currentUserID;
            $bill_obj->printTaskList_time = $G_PGLOB->sys_datetime;
            if (!$G_PGLOB->DBCA->save( $bill_obj )) {
                returnJsonData('配送任务清单打印失败', 404);
            }

        }

        returnJsonData( 'ok', 200 );

    }

    /*
     * 判断订单是否是澳粒订单
    */
    public function check_AL(){
        global $G_PGLOB;

        //$_REQUEST['printBillNoArr']订单商品流水号数组
        if (!isset($_REQUEST['printBillNoArr'][0])) {
            returnJsonData( '参数错误',400);
        }

        $bill_array = $_REQUEST['printBillNoArr'];

        foreach($bill_array as &$bill_no) {

            $bill_obj = new entity('bill_master');
            $bill_obj ->bill_no = $bill_no;
            if(!$G_PGLOB->DBCA->fetch($bill_obj)){
                returnJsonData( '订单不存在' , 404 );
            }

            if( $bill_obj->billPlatform != 2 ){
                returnJsonData( '请选择澳粒订单进行操作' , 400 );
            }
        }

        returnJsonData( 'ok', 200 );
    }


    /**
     *  澳粒订单打印
     */
    public function print_AL(){

        global $G_PGLOB;

        //$_REQUEST['printBillNoArr']订单商品流水号数组
        if (!isset($_REQUEST['printBillNoArr'][0])) {
            returnJsonData( '参数错误',400);
        }

        $bill_array = $_REQUEST['printBillNoArr'];

        foreach($bill_array as &$bill_no) {

            $bill_obj = new entity('bill_master');
            $bill_obj ->bill_no = $bill_no;
            if(!$G_PGLOB->DBCA->fetch($bill_obj)){
                returnJsonData( '订单不存在' , 404 );
            }
            $bill_obj->print_num = $bill_obj->print_num + 1; //打印次数+1
            $bill_obj->print_operator = $G_PGLOB->currentUserID;
            $bill_obj->print_time = $G_PGLOB->sys_datetime;

            if (!$G_PGLOB->DBCA->save($bill_obj)) {
                returnJsonData('订单打印失败', 404);
            }

            /*=======================================================
           * 写入日志 start
           * ======================================================
           */
            quanli_include('module/base/customer/logHandler.class.php');
            //实例日志类
            $logHandler = new logHandler( $G_PGLOB->DBCA );

            $log_array = array(
                'logCategoryID' => 1,
                'logItem' => $bill_obj->bill_no,
                'extend_1' => '澳粒订单打印',
                'extend_2' => '澳粒配送单已打印' . $bill_obj->print_num . '次',
                'extend_5' => $bill_obj->bill_status,
                'operator' => $G_PGLOB->currentUserName

            );

            $logHandler->saveLogInfo( $log_array );
            /*=======================================================
             * 写入日志 end
             * ======================================================
             */

        }

        returnJsonData( 'ok', 200 );

    }

    /*
     * 判断订单是否是自营订单
    */
    public function checkSelforder(){
        global $G_PGLOB;

        //$_REQUEST['printBillNoArr']订单商品流水号数组
        if (!isset($_REQUEST['printBillNoArr'][0])) {
            returnJsonData( '参数错误',400);
        }

        $bill_array = $_REQUEST['printBillNoArr'];

        foreach($bill_array as &$bill_no) {

            $bill_obj = new entity('bill_master');
            $bill_obj ->bill_no = $bill_no;
            if(!$G_PGLOB->DBCA->fetch($bill_obj)){
                returnJsonData( '订单不存在' , 404 );
            }

            if( $bill_obj->billPlatform != 1 ){
                returnJsonData( '请选择自营订单进行操作' , 400 );
            }
        }

        returnJsonData( 'ok', 200 );
    }


    /*
     * 判断订单是否是下午茶订单
    */
    public function check_dunch(){
        global $G_PGLOB;

        if (!isset($_REQUEST['printBillNoArr'][0])) {
            returnJsonData( '参数错误',400);
        }

        $bill_array = $_REQUEST['printBillNoArr'];

        foreach($bill_array as &$bill_no) {

            $bill_obj = new entity('bill_master');
            $bill_obj ->bill_no = $bill_no;
            if(!$G_PGLOB->DBCA->fetch($bill_obj)){
                returnJsonData( '订单不存在' , 404 );
            }


            if( $bill_obj->billPlatform != 3 ){
                returnJsonData( '请选择下午茶订单进行操作' , 400 );
            }
        }

        returnJsonData( 'ok', 200 );
    }


    /**
     *  下午茶订单打印
     */
    public function print_dunch(){

        global $G_PGLOB;

        //$_REQUEST['printBillNoArr']订单商品流水号数组
        if (!isset($_REQUEST['printBillNoArr'][0])) {
            returnJsonData( '参数错误',400);
        }

        $bill_array = $_REQUEST['printBillNoArr'];

        foreach($bill_array as &$bill_no) {

            $bill_obj = new entity('bill_master');
            $bill_obj ->bill_no = $bill_no;
            if(!$G_PGLOB->DBCA->fetch($bill_obj)){
                returnJsonData( '订单不存在' , 404 );
            }
            $bill_obj->print_num = $bill_obj->print_num + 1; //打印次数+1
            $bill_obj->print_operator = $G_PGLOB->currentUserID;
            $bill_obj->print_time = $G_PGLOB->sys_datetime;

            if (!$G_PGLOB->DBCA->save($bill_obj)) {
                returnJsonData('订单打印失败', 404);
            }

            /*=======================================================
           * 写入日志 start
           * ======================================================
           */
            quanli_include('module/base/customer/logHandler.class.php');
            //实例日志类
            $logHandler = new logHandler( $G_PGLOB->DBCA );

            $log_array = array(
                'logCategoryID' => 1,
                'logItem' => $bill_obj->bill_no,
                'extend_1' => '下午茶订单打印',
                'extend_2' => '下午茶配送单已打印' . $bill_obj->print_num . '次',
                'extend_5' => $bill_obj->bill_status,
                'operator' => $G_PGLOB->currentUserName

            );

            $logHandler->saveLogInfo( $log_array );
            /*=======================================================
             * 写入日志 end
             * ======================================================
             */

        }

        returnJsonData( 'ok', 200 );

    }

}
